package com.smartservice.ui.custom_lv_adapters;

import java.util.ArrayList;
import java.util.TreeSet;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.muachung.R;


public class lvAdapterMenu extends BaseAdapter {

	private static final int TYPE_ITEM = 0;
	private static final int TYPE_SEPARATOR = 1;
	private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

	private ArrayList<String> mData = new ArrayList<String>();
	private LayoutInflater mInflater;
	private Context context;

	private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

	public lvAdapterMenu(Context context) {
		this.context = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void addItem(final String item) {
		mData.add(item);
		notifyDataSetChanged();
	}

	/*public void addImageItem(final int item) {
		iData.add(item);
		notifyDataSetChanged();
	}*/

	public void addSeparatorItem(final String item) {
		mData.add(item);
		// save separator position
		mSeparatorsSet.add(mData.size() - 1);
		notifyDataSetChanged();
	}

	@Override
	public int getItemViewType(int position) {
		return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
	}

	@Override
	public int getViewTypeCount() {
		return TYPE_MAX_COUNT;
	}

	public int getCount() {
		return mData.size();
	}

	public String getItem(int position) {
		return mData.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		int type = getItemViewType(position);
		if (convertView == null) {
			holder = new ViewHolder();
			switch (type) {
			case TYPE_ITEM:
				convertView = mInflater.inflate(R.layout.lv_item_menu, null);
				holder.textView = (TextView) convertView
						.findViewById(R.id.text);
				holder.imageDetails = (ImageView) convertView
						.findViewById(R.id.img_detail);
				holder.imageIcon = (ImageView) convertView
						.findViewById(R.id.item_list_menu);
				Resources res = context.getResources();
			//	Button btnChon=(Button) convertView.findViewById(R.id.button1);
//				btnChon.setOnClickListener(new OnClickListener() {
//					
//					@Override
//					public void onClick(View v) {
//						// TODO Auto-generated method stub
//						if(position == 2)
//						Toast.makeText(context, "sdfdsf", Toast.LENGTH_SHORT).show();
//					}
//				});
				if (position == 0) {
					holder.imageIcon.setImageResource(R.drawable.icon);
				} else {
					holder.imageIcon.setImageResource(R.drawable.img_btn_back);
				}
				break;
			case TYPE_SEPARATOR:
				convertView = mInflater.inflate(R.layout.lv_item_menu_section,
						null);
				holder.textView = (TextView) convertView
						.findViewById(R.id.textSeparator);
				break;
			}
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.textView.setText(mData.get(position));
		// holder.imageIcon.setImageResource(iData.get(position));
		return convertView;
	}

	public static class ViewHolder {
		public TextView textView;
		public ImageView imageDetails;
		public ImageView imageIcon;
	}
}
